using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Linq;

namespace OpenWaves.Web.Controls
{
    [ParseChildren(true, "Breakpoints")]
    public class ResponsiveImage : ImageBase
    {
        private readonly ImageBreakpointCollection breakpoints = new ImageBreakpointCollection();
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public ImageBreakpointCollection Breakpoints
        {
            get { return this.breakpoints; }
        }

        protected ImageBreakpoint DefaultBreakpoint
        {
            get { return this.Breakpoints.FirstOrDefault(); }
        }

        protected override void RenderImage(string imageUrl, HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(imageUrl))
            {
                if (this.HideIfEmpty)
                    return;
                
                if (this.DefaultBreakpoint != null)
                {
                    this.RenderImageTag(
                        imageUrl,
                        this.DefaultBreakpoint.Transformation ?? this.Transformation,
                        this.CustomTransformation,
                        this.DefaultBreakpoint.Width,
                        this.DefaultBreakpoint.Height,
                        writer);
                }
            }
            else
            {
                var js = new JavaScriptSerializer();
                writer.RenderBeginTag(HtmlTextWriterTag.Script);

                var transformedImageUrl = this.GetTransformedImageUrl(imageUrl);

                var attributes = new Dictionary<string, string>();
                foreach (string key in this.Attributes.Keys)
                {
                    attributes.Add(key, this.Attributes[key]);
                }

                if (string.IsNullOrEmpty(this.CssClass) == false)
                    attributes.Add("class", this.CssClass);

                attributes.Add("alt", this.AlternateText);

                if (string.IsNullOrEmpty(this.ToolTip) == false)
                    attributes.Add("title", this.ToolTip);

                writer.Write("ow.responsiveImg({0})",
                             js.Serialize(new
                                 {
                                     url = transformedImageUrl.ToString(),
                                     attributes
                                 }));

                writer.RenderEndTag();
            }

            if (this.DefaultBreakpoint != null)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Noscript);
                this.RenderImageTag(
                    imageUrl,
                    this.DefaultBreakpoint.Transformation ?? this.Transformation,
                    this.CustomTransformation,
                    this.DefaultBreakpoint.Width,
                    this.DefaultBreakpoint.Height,
                    writer);
                writer.RenderEndTag();
            }
        }

        private Url GetTransformedImageUrl(string imageUrl)
        {
            var imageTransformationService = ServiceLocator.Resolve<IResponsiveWebImageTransformationService>();

            var transformations = this.Breakpoints.ToDictionary(
                b => b.Name, 
                b => (b.Transformation ?? this.Transformation).CreateImageTransformation(this.CustomTransformation, b.Width, b.Height));

            var transformedImageUrl = imageTransformationService.GetTransformedImageUrl(
                Url.Parse(imageUrl),
                transformations);

            return transformedImageUrl;
        }
    }
}